#########################################################################
##
## This file is part of the SAMRAI distribution.  For full copyright 
## information, see COPYRIGHT and LICENSE. 
##
## Copyright:     (c) 1997-2019 Lawrence Livermore National Security, LLC
## Description:   makefile for Convection-Diffusion ODE sample application 
##
#########################################################################

SAMRAI        = @top_srcdir@
SRCDIR        = @srcdir@
SUBDIR        = source/test/applications/ConvDiff
VPATH         = @srcdir@
TESTTOOLS     = ../../testtools
OBJECT        = ../../../..
REPORT        = $(OBJECT)/report.xml
TESTLIBDIR    = $(OBJECT)/source/test/testlib
TESTLIB       = $(TESTLIBDIR)/libSAMRAI_test$(LIB_SUFFIX)

default: check	

include $(OBJECT)/config/Makefile.config

SUBDIRS = fortran

CPPFLAGS_EXTRA = -DTESTING=1

NUM_TESTS = 2

TEST_NPROCS = @TEST_NPROCS@
QUOTE = \"

CXX_OBJS      = main.o ConvDiff.o MainRestartData.o
F77_OBJS      = fortran/compute_rhs2d.o \
		fortran/init2d.o \
		fortran/rkstep2d.o \
		fortran/tag_cells2d.o \
		fortran/compute_rhs3d.o \
		fortran/init3d.o \
		fortran/rkstep3d.o \
		fortran/tag_cells3d.o


main:	$(CXX_OBJS) $(F77_OBJS) $(LIBSAMRAIDEPEND) $(TESTLIB)
	(cd $(TESTLIBDIR) && $(MAKE) library) || exit 1
	for DIR in $(SUBDIRS); do if test -d $$DIR; then (cd $$DIR && $(MAKE) $@) ; fi || exit 1; done
	$(CXX) $(CXXFLAGS) $(LDFLAGS) $(CXX_OBJS) $(F77_OBJS) $(TESTLIB) \
	$(LIBSAMRAI3D) $(LIBSAMRAI) $(LDLIBS) -o main

# Prevents "No rule to make target" error.  Built in the rule for main.
$(TESTLIB):

fortran/compute_rhs2d.o:

fortran/init2d.o:

fortran/rkstep2d.o:

fortran/tag_cells2d.o:

fortran/compute_rhs3d.o:

fortran/init3d.o:

fortran/rkstep3d.o:

fortran/tag_cells3d.o:

check:
	$(MAKE) check2d
	$(MAKE) check3d

check2d:	main
	@for p in `echo "$(TEST_NPROCS)" | tr "," " "`; do \
	  echo "    <testcase classname=\"applications ConvDiff\" name=$(QUOTE)2d $$p procs$(QUOTE)>" >> $(REPORT); \
	  $(OBJECT)/config/serpa-run $$p ./main test_inputs/test.2d.input | $(TEE) foo; \
	  if ! grep "PASSED" foo >& /dev/null ; then echo "      <failure/>" >> $(REPORT); fi; \
	  echo "    </testcase>" >> $(REPORT); \
	done; \
	$(RM) foo

check3d:	main
	@for p in `echo "$(TEST_NPROCS)" | tr "," " "`; do \
	  echo "    <testcase classname=\"applications ConvDiff\" name=$(QUOTE)3d $$p procs$(QUOTE)>" >> $(REPORT); \
	  $(OBJECT)/config/serpa-run $$p ./main test_inputs/test.3d.input | $(TEE) foo; \
	  if ! grep "PASSED" foo >& /dev/null ; then echo "      <failure/>" >> $(REPORT); fi; \
	  echo "    </testcase>" >> $(REPORT); \
	done; \
	$(RM) foo

checkcompile: main

checktest:
	$(RM) makecheck.logfile
	$(MAKE) check 2>&1 | $(TEE) makecheck.logfile
	$(TESTTOOLS)/testcount.sh $(TEST_NPROCS) $(NUM_TESTS) 0 makecheck.logfile
	$(RM) makecheck.logfile

examples2d: main
	@for i in example_inputs/*.2d.input ; do	\
	  $(OBJECT)/config/serpa-run $(TEST_NPROCS) \
		./main $${i};	\
	done

examples3d: main
	@for i in example_inputs/*.3d.input ; do	\
	  $(OBJECT)/config/serpa-run $(TEST_NPROCS) \
		./main $${i};	\
	done

examples:
	$(MAKE) examples2d
	$(MAKE) examples3d

perf:

everything:
	$(MAKE) checkcompile || exit 1
	$(MAKE) checktest
	$(MAKE) examples
	$(MAKE) perf

checkclean:
	$(CLEAN_COMMON_CHECK_FILES)
	$(RM) test*.restart

clean: checkclean
	$(CLEAN_COMMON_TEST_FILES)
	$(RM) main
	for DIR in $(SUBDIRS); do if test -d $$DIR; then (cd $$DIR && $(MAKE) $@) ; fi || exit 1; done

redo:
	$(RM) core main*d

include $(SRCDIR)/Makefile.depend

AutoTester.o:	$(AUTOTEST)/AutoTester.C
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $(AUTOTEST)/AutoTester.C -o $@

